package service

import (
	dao2 "Wechat/dao"
	domain2 "Wechat/domain"
	"reflect"
)

// 创建一个ForumService进行函数调用
type ForumService string

// 创建一个ForumMapper用于调用数据库
var forummapper dao2.ForumDataMapper

// 添加帖子
func (f ForumService) AddText(forum domain2.Forum) bool {
	var forumid int
	text := forummapper.SelectNewText()
	if reflect.DeepEqual(text, domain2.Forum{}) {
		forumid = 1
	} else {
		forumid = text.ID + 1
	}
	if forummapper.InsertText(forum.Title, forum.UnionID, forum.PicPath, forum.Detail, forum.VideoPath, forumid) > 0 {
		return true
	} else {
		return false
	}
}

// 添加评论
func (f ForumService) AddComment(forum domain2.Forum) bool {
	//	看能不能找到帖子
	if reflect.DeepEqual(forummapper.SelectText(forum.ID), domain2.Forum{}) {
		return false
	}
	//	找得到那就插入
	if forummapper.InsertComment(forum.Detail, forum.UnionID, forum.ID) > 0 {
		return true
	} else {
		return false
	}
}

//获取帖子详情
func (f ForumService) GetTextDetail(forumid int) (bool, domain2.Forum) {
	forum := forummapper.SelectText(forumid)
	if reflect.DeepEqual(forum, domain2.Forum{}) {
		return false, domain2.Forum{}
	} else {
		return true, forum
	}
}

//获取帖子列表
func (f ForumService) GetTextList(forumid int) (bool, []domain2.Forum) {
	if forumid == 1 {
		forumid = 0
	}
	Texts := forummapper.SelectTextList(forumid)
	if len(Texts) == 0 {
		return false, Texts
	} else {
		return true, Texts
	}
}

//获取评论列表
func (f ForumService) GetCommentList(forumid int) (bool, []domain2.Forum) {
	if forumid == 1 {
		forumid = 0
	}
	Comments := forummapper.SelectCommentList(forumid)
	if len(Comments) == 0 {
		return false, Comments
	} else {
		return true, Comments
	}
}
